package com.fenqing.leetcode.code.t0_200.t0_100;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

/**
 * @author fenqing
 */
public class Topic005 extends Topic {


    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic005(String input) {
        super(input);
    }

    @Execute
    public String longestPalindrome(String s) {
        char[] chars = s.toCharArray();
        int len = chars.length;
        int start = 0;
        int end = 0;
        for (int i = 0; i < len; i++) {
            int len1 = expandAroundCenter(chars, i, i);
            int len2 = expandAroundCenter(chars, i, i + 1);
            int max = Math.max(len1, len2);
            if (max > end - start + 1) {
                start = i - (max - 1) / 2;
                end = i + max / 2;
            }
        }
        return s.substring(start, end + 1);
    }

    public int expandAroundCenter(char[] chars, int start, int end){
        while (start >= 0 && end < chars.length && chars[start] == chars[end]){
            start--;
            end++;
        }
        return end - start - 1;
    }



    public static void main(String[] args) {
        Topic005 topic = new Topic005("babad");
        String run = topic.run();
        System.out.println(run);
    }
}
